package com.mocoop.biz;


import java.util.ArrayList;
import java.util.List;

import com.mocoop.activity.MocoopActivity;
import com.mocoop.commom.MyConstants;
import com.mocoop.commom.MyTaskCallBack;
import com.mocoop.dao.SyncDao;
import com.mocoop.dao.SyncDaoImp;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class CheckFirstRunBiz {
	private Context context;
	private String dbName;
	private MyTaskCallBack myTaskCallBack;

	public void setMyTaskCallBack(MyTaskCallBack myTaskCallBack) {
		this.myTaskCallBack = myTaskCallBack;
	}

	

	public CheckFirstRunBiz(Context context, String dbName) {
		super();
		this.context = context;
		this.dbName = dbName;
	}

	
	
	public void checkDB() {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				"share", 0);
		boolean isFirstRun = sharedPreferences.getBoolean(dbName, true);

		if (isFirstRun) {
		
			//如果用户第一次登录   创建以下9张表	
			final SyncDao syncDataDao = new SyncDaoImp(context,dbName);
				//ajaxworker表
				syncDataDao.createTable("CREATE TABLE  T_CloudDataTransferCenter (" +
						"FCloudDataTransferID INTEGER PRIMARY KEY AUTOINCREMENT," +
						"FCloudDataTransferURL TEXT NOT NULL," +
						"FCloudDataTransferHTTPMethod TEXT NOT NULL, " +
						"FCloudDataTransferRequestData TEXT, " +
						"FCloudDataTransferUploadAttachmentFilesInfo TEXT," +
						"FCloudDataTransferCreatedTimeStamp bigint(20) NOT NULL, " +
						"FCloudDataTransferEndTimeStamp bigint(20)," +
						"FCloudDataTransferState TEXT Default('Waiting'), " +
						"FCloudDataTransferResponseData TEXT, " +
						"FCloudDataTransferErrorMessage TEXT, " +
						"FCloudDataTransferExtra TEXT," +
						"FCloudDataTransferSQLOperation TEXT," +
						"FCloudDataTransferNoticePages TEXT," +
						"FCloudDataTransferMatchIDs TEXT" +
						" );");
				 //附件下载表
				syncDataDao.createTable("Create  TABLE  T_ImgFilePaths ([FImgFileKey] TEXT UNIQUE NOT NULL,[FImgFilePath] TEXT NOT NULL);");
				//业务表
				syncDataDao.createTable(MyConstants.QC_ABNORMAL);
				syncDataDao.createTable(MyConstants.QC_ORDER);
				syncDataDao.createTable(MyConstants.QC_PARTY);
				syncDataDao.createTable(MyConstants.QC_PROCESS);
				syncDataDao.createTable(MyConstants.QC_REMARK);
				syncDataDao.createTable(MyConstants.QC_DYNAMIC);
				syncDataDao.createTable(MyConstants.QC_MESSAGE);
				
				
				//同步表数据
				List<String> tables = new ArrayList<String>();
				tables.add("QC_ORDER");
				tables.add("QC_PROCESS");
				tables.add("QC_REMARK");
				tables.add("QC_ABNORMAL");
				tables.add("QC_PARTY");
				tables.add("QC_DYNAMIC");
				tables.add("QC_MESSAGE");
				
				
				SyncDataBiz syncDataBiz = new SyncDataBiz();
				syncDataBiz.setSyncAllDataCallBack(new SyncDataBiz.SyncDataCallBack() {
					
					@Override
					public void onSuccess(int batchCount) {
						//更改一下几张表的未读字段为已读
						syncDataDao.execute("update QC_REMARK set unread = 'F';", new String[0]);
						syncDataDao.execute("update QC_ABNORMAL set unread = 'F';", new String[0]);
						syncDataDao.execute("update QC_MESSAGE set unread = 'F';", new String[0]);
						syncDataDao.execute("update QC_DYNAMIC set unread = 'F';", new String[0]);
						myTaskCallBack.onSuccess("");
					}
				});
				syncDataBiz.loadDataByList((MocoopActivity)context, tables, dbName);
				
				Editor editor = sharedPreferences.edit();
				editor.putBoolean(dbName, false);
				editor.commit();
				
		} else {
			if(myTaskCallBack!=null)
			myTaskCallBack.onSuccess("");
		}
	}
}